﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.Diagnostics;
using System.Drawing;

namespace HaiTaoTools
{
    public class Weather
    {
        /// <summary>
        /// 从远程地址获取XML结构
        /// </summary>
        /// <param name="city">城市英文</param>
        /// <returns></returns>
        public static XmlDocument GoogleWeatherAPI(string city)
        {
            string baseUrl = "http://www.google.com/ig/api?hl=zh-cn&weather=" + city;
            HttpWebRequest gg_Request;
            HttpWebResponse gg_Response = null;
            XmlDocument gg_XMLdoc = null;
            try
            {
                gg_Request = (HttpWebRequest)WebRequest.Create(string.Format(baseUrl));
                gg_Request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.4) Gecko/20100413 Firefox/3.6.4";
                gg_Response = (HttpWebResponse)gg_Request.GetResponse();
                gg_XMLdoc = new XmlDocument();
                gg_XMLdoc.Load(gg_Response.GetResponseStream());
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            gg_Response.Close();
            return gg_XMLdoc;

        }

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="picPath"></param>
        /// <returns></returns>
        public static Image GetPicImage(string picPath)
        {
            Bitmap bit;
            Image imagePic=null;
            HttpWebRequest gg_Request;
            HttpWebResponse gg_Response = null;
            try
            {

                gg_Request = (HttpWebRequest)WebRequest.Create(picPath);
                gg_Response = (HttpWebResponse)gg_Request.GetResponse();
                bit = new Bitmap(gg_Response.GetResponseStream());
                imagePic = Image.FromHbitmap(bit.GetHbitmap());
                gg_Response.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return imagePic;
        }
    }
}
